package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;
import com.yqg.education.entity.TrainSign;
import com.yqg.education.entity.User;
import com.yqg.education.mapper.TrainSignMapper;
import com.yqg.education.service.IPeriodService;
import com.yqg.education.service.ITrainSignService;
import com.yqg.education.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * TrainSignController 前端控制器
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/train-sign")
public class TrainSignController {

    @Resource
    private ITrainSignService trainSignService;

    @Resource
    private TrainSignMapper trainSignMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody TrainSign trainSign) {
        if (trainSign.getId() != null) {
            TrainSign existingLog = trainSignService.getById(trainSign.getId());
            TrainSign updateLog = new TrainSign();
            updateLog.setId(trainSign.getId());
            updateLog.setTime(trainSign.getTime());
            if (existingLog != null && existingLog.getFlow() == 0) {
                updateLog.setFlow(trainSign.getFlow());
            }
            trainSignService.updateById(updateLog);
            return Result.success(updateLog);
        }
        trainSign.setCreateTime(LocalDateTime.now());
        trainSignService.saveOrUpdate(trainSign);
        return Result.success(trainSign);
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        TrainSign trainSign =trainSignService.getById(id);
        return Result.success(trainSignService.saveOrUpdate(trainSign));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<TrainSign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<TrainSign> list=  trainSignService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(trainSignService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                           String username,
                           String courseName,
                           String periodName,
                           @RequestParam(required = false) Integer flow,
                           @RequestParam(required = false) Integer stuId) {
        pageNum = pageNum * pageSize - pageSize;
        List<TrainSign> trainSignList = trainSignMapper.findPage(pageNum, pageSize, username, courseName, periodName, flow, stuId);
        Long count = trainSignMapper.findCount(username, courseName, periodName, flow, stuId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", trainSignList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 学员端查询是否已签到接口
     */
    @GetMapping("/stuFindIsTrainSign")
    public Result stuFindIsTrainSign(@RequestParam Long periodId,
                                 @RequestParam Long stuId){
        Map<String,Object> map= trainSignService.stuFindIsTrainSign(periodId,stuId);
        return Result.success(map);
    }
    /**
     * 修改学员端观看视频完成度flow
     */
    @GetMapping("/stuUpdateTrainSignFlow")
    public Result stuUpdateTrainSignFlow(@RequestParam Integer periodId,
                                         @RequestParam Integer stuId){
        trainSignMapper.stuUpdateTrainSignFlow(periodId,stuId);
        return Result.success();
    }

    @GetMapping("/findAllByUserId")
    public Result findAllByUserId(@RequestParam Integer userId, @RequestParam Integer periodId) {
        QueryWrapper<TrainSign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id", userId);
        queryWrapper.eq("period_id", periodId);
        queryWrapper.eq("is_delete", 0);
        return Result.success(trainSignService.getOne(queryWrapper));
    }
}

